package cn.demarcia.coco.uihelper;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import static android.content.SharedPreferences.Editor;

/**
 * 本地存储工具类
 * Created by 智尧 on 2016/3/15.
 */
public class SharedPreferenceUtils {
    private static SharedPreferenceUtils mSpUtilsInstance = null;
    private static final String SHAREDPREFERENCE_FILE_NAME = "wuxiu";

    /**
     * SharedPreference key值
     */
    public static final String LOGIN_USER_LAST_MOBILE = "loginUserLastMobile";
    public static final String ACCESS_TOKEN_KEY = "lastAccessToken";
    public static final String REFRESH_TOKEN_KEY = "lastRefreshToken";
    public static final String IM_ACCESS_TOKEN_KEY = "imAccessToken";
    /**
     * SharedPreferences Value值
     */
    private static SharedPreferences mSPreferences;

    public SharedPreferenceUtils() {
    }

    public void putString(String key, String value) {
        Editor editor = mSPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public String getString(String key, String defValue) {
        return mSPreferences.getString(key, defValue);
    }

    public void putBoolean(String key, boolean value) {
        Editor editor = mSPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public boolean getBoolean(String key, boolean defValue) {
        return mSPreferences.getBoolean(key, defValue);
    }

    public void remove(String key) {
        Editor editor = mSPreferences.edit();
        editor.remove(key);
        editor.apply();
    }

    public synchronized static SharedPreferenceUtils getInstance(Context context) {
        if (mSpUtilsInstance == null) {
            mSpUtilsInstance = new SharedPreferenceUtils();
        }
        if (mSPreferences == null) {
            mSPreferences = context
                    .getSharedPreferences(SHAREDPREFERENCE_FILE_NAME, Activity.MODE_PRIVATE);
        }
        return mSpUtilsInstance;
    }
}
